package com.bupt.constants;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;

import java.io.Serializable;

@Data
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) // 返回值为null的不进行展示
public class ResponseResult<D> implements Serializable {

    private static final long serialVersionUID = -5290436355400748988L;

    private int respCode;// 返回的操作码  200：说明操作成功  500：说明操作失败
    private String message; // 返回的信息描述
    private D data; // 响应的数据的基本返回

    /*  私有构造方法 防止用户自己new 对象   */
    private ResponseResult() {
    }

    private ResponseResult(int respCode, String message) {
        this.respCode = respCode;
        this.message = message;
    }

    private ResponseResult(int respCode, D data) {
        this.respCode = respCode;
        this.data = data;
    }

    private ResponseResult(int respCode, String message, D data) {
        this.respCode = respCode;
        this.message = message;
        this.data = data;
    }

    // 返回默认的成功对象
    @Deprecated
    public static ResponseResult<String> createSuccessResult() {
        return new ResponseResult<>(RetCodeConstant.SUCCESS.getCode(),
                RetCodeConstant.SUCCESS.getMsg());
    }

    // 返回带有特定信息的成功对象
    public static ResponseResult<String> createSuccessResult(String message) {
        return new ResponseResult<>(RetCodeConstant.SUCCESS.getCode(), message);
    }

    // return ok ： 推荐使用
    public static <D> ResponseResult<D> createOK(D data) {
        // return ok
        return createSuccessResult("操作成功", data);
    }

    public static <D> ResponseResult<D> createSuccessResult(String message, D data) {
        return createResult(RetCodeConstant.SUCCESS, message, data);
    }

    // 返回默认的失败对象
    @Deprecated
    public static ResponseResult<String> createFailResult() {
        return new ResponseResult<>(RetCodeConstant.FAIL.getCode(),
                RetCodeConstant.FAIL.getMsg());
    }

    // 返回带有特定信息的失败对象
    @Deprecated
    public static ResponseResult<String> createFailResult(String message) {
        return new ResponseResult<>(RetCodeConstant.FAIL.getCode(), message);
    }

    // 返回带有数据的失败对象
    public static <D> ResponseResult<D> createFailResult(D data) {
        return createFailResult("操作失败", data);
    }

    public static <D> ResponseResult<D> createFailResult(String message, D data) {
        return createResult(RetCodeConstant.FAIL, message, data);
    }

    // 推荐使用这个
    public static <D> ResponseResult<D> createFail(D data) {
        return createFailResult("操作失败", data);
    }

    public static <D> ResponseResult<D> createResult(RetCodeConstant retCodeConstant, String message, D data) {
        return new ResponseResult<D>(retCodeConstant.getCode(), message, data);
    }

    public enum RetCodeConstant {

        SUCCESS(200, "成功"),
        NOT_LOGIN(300, "未登录"),
        EXCEPTION(400, "请求发生异常"),
        FAIL(500, "失败"),
        TO_MANNY_REQUEST(600, "不能在1s内连续请求该接口"),
        ALERT(700, "弹出提示");
        // return code
        private int code;
        // return message
        private String msg;

        private RetCodeConstant(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public int getCode() {
            return code;
        }

        private void setCode(int code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

        private void setMsg(String msg) {
            this.msg = msg;
        }
    }
}
